/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.partytype.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.modules.partytype.entity.PartyType;
import com.fosung.frame.modules.partytype.service.PartyTypeService;

/**
 * 党组织类型Controller
 * @author wu
 * @version 2017-08-29
 */
@Controller
@RequestMapping(value = "${adminPath}/partytype/partyType")
public class PartyTypeController extends BaseController {

	@Autowired
	private PartyTypeService partyTypeService;
	
	@ModelAttribute
	public PartyType get(@RequestParam(required=false) String id) {
		PartyType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = partyTypeService.get(id);
		}
		if (entity == null){
			entity = new PartyType();
		}
		return entity;
	}
	
	@RequiresPermissions("partytype:partyType:view")
	@RequestMapping(value = {"list", ""})
	public String list(PartyType partyType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PartyType> page = partyTypeService.findPage(new Page<PartyType>(request, response), partyType); 
		model.addAttribute("page", page);
		return "modules/partytype/partyTypeList";
	}

	@RequiresPermissions("partytype:partyType:view")
	@RequestMapping(value = "form")
	public String form(PartyType partyType, Model model) {
		model.addAttribute("partyType", partyType);
		return "modules/partytype/partyTypeForm";
	}

	@RequiresPermissions("partytype:partyType:edit")
	@RequestMapping(value = "save")
	public String save(PartyType partyType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partyType)){
			return form(partyType, model);
		}
		partyTypeService.save(partyType);
		addMessage(redirectAttributes, "保存党组织类型成功");
		return "redirect:"+Global.getAdminPath()+"/partytype/partyType/?repage";
	}
	
	@RequiresPermissions("partytype:partyType:edit")
	@RequestMapping(value = "delete")
	public String delete(PartyType partyType, RedirectAttributes redirectAttributes) {
		partyTypeService.delete(partyType);
		addMessage(redirectAttributes, "删除党组织类型成功");
		return "redirect:"+Global.getAdminPath()+"/partytype/partyType/?repage";
	}
	@RequiresPermissions("partytype:partyType:edit")
	@RequestMapping(value = "updateStart")
	public String updateStart(PartyType partyType, RedirectAttributes redirectAttributes) {
		partyTypeService.updateStart(partyType);
		addMessage(redirectAttributes, "启动党组织类型成功");
		return "redirect:"+Global.getAdminPath()+"/partytype/partyType/?repage";
	}
	@RequiresPermissions("partytype:partyType:edit")
	@RequestMapping(value = "updateStop")
	public String updateStop(PartyType partyType, RedirectAttributes redirectAttributes) {
		partyTypeService.updateStop(partyType);
		addMessage(redirectAttributes, "关闭党组织类型成功");
		return "redirect:"+Global.getAdminPath()+"/partytype/partyType/?repage";
	}
	@ResponseBody
	@RequiresPermissions("partytype:partyType:edit")
	@RequestMapping(value = "checkName")
	public String checkName(String oldName, String name) {
		PartyType partyType=new PartyType();
		partyType.setName(name);
		if (name !=null && name.equals(oldName)) {
			return "true";
		} else if (name !=null && partyTypeService.check(partyType) == null) {
			return "true";
		}
		return "false";
	}
	
}